﻿using Sharp.DataTransferObject;
using Sharp.Infrastructure.WebApiClient;
using Sharp.Portal.Mvc;
using Sharp.ServiceContracts;
using Sharp.WebBooter.Areas.Platform.Models;
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
using System.Web.Mvc;
using Sharp.Infrastructure.Search;

// ReSharper disable Mvc.ViewNotResolved

namespace Sharp.WebBooter.Areas.Platform.Controllers
{
    /// <inheritdoc />
    /// <summary>
    /// 员工
    /// </summary>
    [Authorize]
    [AutomaticTemplate]
    public class EmployeeController : SharpController
    {
        /// <summary>
        /// 入口
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            var vm = new EmployeeIndexModel();

            return View(vm);
        }

        /// <summary>
        ///     分页数据
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ActionResult GetPaged(DataTableParameter parameters)
        {
            using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
            {
                var pageInfo = proxy.Channel.GetEmployeePaged(parameters.ToPagingFeature());

                return this.Store(pageInfo);
            }
        }

        public ActionResult GetEmployees(Guid[] orgids)
        {
            using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
            {
                var pageInfo = proxy.Channel.GetEmployeesInOrg(orgids);

                return this.JsonNet(pageInfo);
            }
        }

        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            EmployeeDTO dto;
            using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
            {
                dto = proxy.Channel.GetEmployeeInfo(id.Value);
            }
            if (dto == null)
            {
                return HttpNotFound();
            }

            return View(dto.To<EmployeeModel>());
        }

        [Permission("新增", 1, "003001001")]
        public async Task<ActionResult> Create()
        {
            var vm = new EmployeeModel();
            IEnumerable<OrgDTO> allOrgs;
            using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
            {
                allOrgs = await proxy.Channel.GetOrgs();
            }
            OrgController.Build(allOrgs, vm.OrgIdDataSource, null);

            return View(vm);
        }


        [Permission("编辑", 2, "003001002")]
        public async Task<ActionResult> Edit(int? id)
        {
            EmployeeDTO dto;
            using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
            {
                dto = proxy.Channel.GetEmployeeInfo(id.Value);
            }


            EmployeeModel vm = dto.To<EmployeeModel>();
            if (dto == null)
            {
                vm = new EmployeeModel();
            }
            IEnumerable<OrgDTO> allOrgs;
            using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
            {
                allOrgs = await proxy.Channel.GetOrgs();
            }
            OrgController.Build(allOrgs, vm.OrgIdDataSource, null);

            return View(vm);
        }


        
        public ActionResult Save(EmployeeModel user)
        {
            EmployeeDTO dto = user.To<EmployeeDTO>();

            using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
            {
                var res = proxy.Channel.SaveEmployee(dto);

                return this.JsonNet(res);
            }
        }
        [Permission("删除", 3, "003001003")]
        
        public ActionResult Delete(int[] ids)
        {
            using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
            {
                var res = proxy.Channel.DeleteEmployee(ids);

                return this.JsonNet(res);
            }

        }
        [Permission("设为用户", 4, "003001004")]
        [System.Web.Mvc.HttpGet]
        public ActionResult AsUser(int? employeeId)
        {
            var model = new UserViewModel();

            return View("AsUser", model);
        }
        
        [Permission("设为用户", 4, "003001004")]
        public ActionResult AsUser(UserViewModel vm, int? employeeId)
        {
            using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
            {
                var res = proxy.Channel.EmployeeAsUser(vm.To<UserDTO>(), employeeId.Value);

                return this.JsonNet(res);
            }
        }

        public ActionResult EmployeeDialog(string selectTextControl)
        {
            ViewBag.SelectTextControl = selectTextControl;
            return View("EmployeeDialog");
        }
    }
}